/*
 * File:   tick.c
 * Author: craigjb
 *
 * Created on April 16, 2013, 10:50 AM
 */

#include "board.h"

volatile unsigned long int g_tickTime;
static const unsigned int kTMR1LoadVal = 
    (unsigned int)(((float)FCY) / 64.0f / 1000.0f);

void InitializeTick() {
    _T1IF = 0;
    _T1IE = 1;
    TMR1 = 0;
    PR1 = kTMR1LoadVal;
    T1CON = 0x8020;
    g_tickTime = 0;
}

unsigned long int GetTick() {
    return g_tickTime;
}

void PauseTick() {
    T1CONbits.TON = 0;
}

void ResumeTick() {
    //TMR1 = 0;
    PR1 = kTMR1LoadVal;
    T1CONbits.TON = 1;
}

void __attribute__((interrupt, auto_psv)) _T1Interrupt(void) {
    _T1IF = 0;
    g_tickTime++;
}
